//
//  YSGNurseAppointMentStarRatingView.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/4/11.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit
import SwifterSwift

protocol YSGNurseAppointMentStarRatingBGViewDelegate: NSObjectProtocol {
    func evaluateAction(_ evaluateString : String,_ stars : String)

}

class YSGNurseAppointMentStarRatingBGView: UIView, UIGestureRecognizerDelegate, StarRatingViewDelegate, YYTextViewDelegate {
    weak var delegate: YSGNurseAppointMentStarRatingBGViewDelegate?
    var evaluateString: String = ""
    var stars : String = "0"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setUI() {
        backgroundColor = UIColor(r: 0/255.0, g: 0/255.0, b: 0/255.0, alpha: 1).withAlphaComponent(0.3)
        alpha = 0
        mainWindow.addSubview(self)
        
        let view = UINib.loadViewFrom(YSGNurseAppointMentStarRatingView.self)
        view.frame = CGRect(x: 0, y: 80, width: screenW, height: 226)
        self.addSubview(view)

        view.layer.cornerRadius = 12


        view.starRatingView.delegate = self
        view.starRatingView.slideEnable = true
        view.starRatingView.clickEnable = true
        view.textView?.delegate = self

        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init()

        tap.numberOfTapsRequired = 1

        tap.addTarget(self, action: #selector(tapAction))

        tap.delegate = self

        self.addGestureRecognizer(tap)


        let button = UIButton(frame: CGRect(x: 25, y: 316, width: screenW-50, height: 50))
        button.layer.cornerRadius = 9
        button.backgroundColor = UIColor.mainColor
        button.setTitleColor(.white, for: .normal)
        button.setTitle("提交评价", for: .normal)
        button.addTarget(self, action: #selector(evaluateButtonAction), for: .touchUpInside)
        self.addSubview(button)
    }
    
    @objc func evaluateButtonAction(){
        
        self.endEditing(true)
        
        if stars.floatValue == 0 {
            HUDTool.show(message: "请填写评价分数")
            return
        }
        
        delegate?.evaluateAction(evaluateString , stars)
        dismiss()
    }
    
    func starRatingView(_ starRatingView: StarRatingView, count: Float) {
        stars  = String(format: "%.0f", count)
    }
    
    func textViewDidEndEditing(_ textView: YYTextView) {
        evaluateString = textView.text
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
         if touch.view != self{
             return false
         }
         return true
     }

    
    
    @objc func tapAction(){
        dismiss()
    }
    func show() {
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 1
        }) { (finish) in
            
        }
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 0
        }) { (finish) in
            if finish {
                self.removeFromSuperview()
            }
        }
    }
}

class YSGNurseAppointMentStarRatingView: UIView {

    @IBOutlet weak var bodyView: UIView!{
        didSet{
            textView = YYTextView.init(frame: CGRect.init(x: 10, y: 10, width: screenW-78, height: 131))
            textView?.placeholderText = "请输入要评价的内容"
            textView?.delegate = self
            textView?.font = UIFont.systemFont(ofSize: 12)
            textView?.textColor = UIColor.textGray
            bodyView.addSubview(textView ?? YYTextView.init(frame:CGRect.zero))
            bodyView.layer.cornerRadius = 16
            bodyView.layer.borderColor = Color.init(hex: "#DDDDDD").cgColor
            bodyView.layer.borderWidth = 1
        }
    }
    
    @IBOutlet weak var titleLabel: UILabel!
    var textView : YYTextView?
    
    @IBOutlet weak var starRatingView: StarRatingView!{
        didSet {
            starRatingView.backgroundColor = .clear
            starRatingView.delegate = self

            starRatingView.wholeStar = true
        }
    }
    
    override class func awakeFromNib() {
        super .awakeFromNib()
    }
}

extension YSGNurseAppointMentStarRatingView :StarRatingViewDelegate,YYTextViewDelegate{
    func starRatingView(_ starRatingView: StarRatingView, count: Float) {
//        if let vm = viewModel {
//            evaluate(orderId: vm.orderId, score: count)
//        }
    }
    func textViewDidEndEditing(_ textView: YYTextView) {
        
    }

}

